﻿using Biz;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading.Tasks;
using Entity;
using DAL;
using Util;
using UtilLibrary;
using System.Collections.Generic;
using System.Linq;

namespace TestProjectForTaskMgr
{
    
    
    /// <summary>
    ///This is a test class for TaskServiceTest and is intended
    ///to contain all TaskServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TaskServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
           
        }
        
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            CreateWorkflowAndService();
        }
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public void TestWorkflow()
        {
        }

        /// <summary>
        ///A test for Start
        ///</summary>
        [TestMethod()]
        public void StartTestNormal()
        {
            s.Start();
            Assert.IsTrue(t.TaskStatus == Util.TaskStatus.Processing);
            Assert.IsTrue(t.Evaluate == ScheduleStatus.Normal);
            Assert.IsTrue(t.StepNo == 1);
            Assert.IsTrue(t.AssignerID == 1, "Test AssingID, the value is: " + t.AssignerID.ToString());
            Assert.IsFalse(s.Start());
        }

        [TestMethod()]
        public void StartTestDelay()
        {   
            t.PlanFinishDate = DateTime.Now.AddDays(-1);
            
            TaskService s = new TaskService(t);
            s.Start();
            Assert.IsTrue(t.TaskStatus == Util.TaskStatus.Processing);
            Assert.IsTrue(t.Evaluate == ScheduleStatus.Delay);
            Assert.IsFalse(s.Start());
        }

        [TestMethod()]
        public void TestNext()
        {
            Assert.IsFalse(s.Next());

            s.Start();
            s.Next();
            Assert.IsTrue(t.TaskStatus == Util.TaskStatus.Processing);
            Assert.IsTrue(t.Evaluate == ScheduleStatus.Normal);
            Assert.IsTrue(t.StepNo == 2, "Test step no: " + t.StepNo);
            Assert.IsTrue(t.AssignerID == 2);
            s.Next();
            Assert.IsTrue(t.TaskStatus == Util.TaskStatus.Processing);
            Assert.IsTrue(t.Evaluate == ScheduleStatus.Normal);
            Assert.IsTrue(t.StepNo == 3);
            Assert.IsTrue(t.AssignerID == 3);
            s.Next();
            Assert.IsTrue(t.TaskStatus == Util.TaskStatus.Finish);
            Assert.IsTrue(t.AssignerID == t.ProviderID);
        }

        private static BizTask t = null;
        private static TaskService s = null;

        private static void CreateWorkflowAndService()
        {
            t = new BizTask();
            t.Id = 1;
            t.TaskName = "TaskForTest";
            t.ProviderID = 5;
            t.PlanFinishDate = DateTime.Now.AddDays(1);
            t.Create();

            BizWorkflow wf = new BizWorkflow(t.Id);
            wf.Id = 1;
            wf.Create();

            wf.AddNode(new BizWorkflowSequeuece(1, 1, "Node1"));
            BizWorkflowSequeuece wfs2 = new BizWorkflowSequeuece(2, 2, "Node2");
            wf.AddNode(wfs2);
            BizWorkflowSequeuece wfs3 = new BizWorkflowSequeuece(3, 3, "Node3");
            wf.AddNode(wfs3);
            t.TaskWF = wf;
            s = new TaskService(t);
        }

        [TestMethod()]
        public void TestNextDelay()
        {
            t.PlanFinishDate = DateTime.Now.AddDays(-1);
            s.Start();
            s.Next();
            Assert.IsTrue(t.Evaluate == ScheduleStatus.Delay);
        }

        //[TestMethod()]
        //public void TestFinish()
        //{
        //    t.Finish();
        //    Assert.IsTrue(t.TaskWF.isLastOne() && t.Evaluate == ScheduleStatus.Finish && t.TaskStatus == Util.TaskStatus.Finish);
        //}

        [TestMethod()]
        public void TestCreateTask()
        {
            BizTask task = new BizTask();
            task.TaskName = "myTask";
            task.TaskDescription = "myDescription";
            task.RealFinishDate = DateTime.Now;
            task.PlanFinishDate = DateTime.Now.AddDays(1);
            DAL.task entity = new DAL.task();
            TaskService service = new TaskService(task);
            service.Create();
            BizTask task2 = (BizTask)TaskService.GetTaskByID(task.Id);
            Assert.IsTrue(task.TaskName == task2.TaskName && task.TaskDescription == task2.TaskDescription);
        }

        [TestMethod]
        public void TestUpdateTask() {
            BizTask task = new BizTask();
            task.TaskName = "myTask123";
            task.TaskDescription = "myDescription";
            task.RealFinishDate = DateTime.Now;
            task.PlanFinishDate = DateTime.Now.AddDays(1);
            DAL.task entity = new DAL.task();
            DataAccessor.InsertData(task);

            task.TaskName = "myTask123Update";
            task.Update();
            task = (BizTask)TaskService.GetTaskByID(task.Id);
            Assert.IsTrue(task.TaskName == "myTask123Update");
        }

        [TestMethod]
        public void TestDeleteTask()
        {
            BizTask bizTask = new BizTask();
            bizTask.TaskName = "myTask123";
            bizTask.TaskDescription = "myDescription";
            bizTask.RealFinishDate = DateTime.Now;
            bizTask.PlanFinishDate = DateTime.Now.AddDays(1);

            TaskService service = new TaskService(bizTask);
            service.Create();
            
            BizTask bizEntity = (BizTask)TaskService.GetTaskByID(bizTask.Id);
            Assert.IsNotNull(bizEntity);
            Assert.IsTrue(bizTask.Id == bizEntity.Id);
            int id = bizTask.Id;

            bizTask.Delete();
            BizTask wt = new BizTask();
            Assert.IsNull(TaskService.GetTaskByID(id));
        }

        [TestMethod]
        public void TestGetTaskByID()
        {
            BizTask bizTask = new BizTask();
            bizTask.TaskName = "myTask123";
            bizTask.TaskDescription = "myDescription";
            bizTask.RealFinishDate = DateTime.Now;
            bizTask.PlanFinishDate = DateTime.Now.AddDays(1);

            TaskService service = new TaskService(bizTask);
            service.Create();

            BizTask bizTask2 = (BizTask)TaskService.GetTaskByID(bizTask.Id);
            Assert.IsNotNull(bizTask2);
            Assert.IsTrue(bizTask.Id == bizTask2.Id);
        }

        [TestMethod]
        public void TestGetAllTask()
        {
            List<BizTask> lst = TaskService.GetAllEntities();
        }
    }
}